package com.project.weimi.adapter;

import java.util.ArrayList;
import java.util.List;

import com.project.weimi.R;
import com.project.weimi.activity.MainActivity;
import com.project.weimi.entity.response.ShopEntity;
import com.project.weimi.widget.ToastMessage;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

public class ShoppingAdapter extends RecyclerView.Adapter<ViewHolder> {
	private List<ShopEntity> list;
	private Context context;

	private View mHeaderView;

	private static final int TYPE_ITEM = 0;
	private static final int TYPE_FOOTER = 1;
	private static final int TYPE_HEADER = 2;

	public List<ShopEntity> getList() {
		return list;
	}

	public ShoppingAdapter(Context context) {
		list = new ArrayList<ShopEntity>();
		this.context = context;
	}

	public void setHeaderView(View headerView) {
		mHeaderView = headerView;
		notifyItemInserted(0);
	}

	public View getHeaderView() {
		return mHeaderView;
	}

	@Override
	public int getItemCount() {
		return list.size() > 3 ? list.size() + 1 : list.size();
	}

	@Override
	public int getItemViewType(int position) {
		if (position == 0 && mHeaderView != null) {
			return TYPE_HEADER;
		} else if (position + 1 == getItemCount() && getItemCount() > 3) {
			return TYPE_FOOTER;
		} else {
			return TYPE_ITEM;
		}
	}

	@Override
	public void onBindViewHolder(final ViewHolder holder, final int position) {
		if (holder instanceof ItemViewHolder) {
			((ItemViewHolder) holder).shop_delete.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub

					AlertDialog dialog = new AlertDialog.Builder(context).setMessage("确认要删除该商品吗？")
							.setNeutralButton("取消", null)
							.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							list.remove(list.get(position));
							ShoppingAdapter.this.notifyDataSetChanged();

							((MainActivity) context).getShopFragment().itemCheckBox();
						}
					}).create();
					dialog.show();
				}
			});
			((ItemViewHolder) holder).shop_less.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (list.get(position).getShop_check_num() > 1) {
						list.get(position).setShop_check_num(list.get(position).getShop_check_num() - 1);
						list.get(position).setShop_num(String.valueOf((list.get(position).getShop_check_num())));
						((ItemViewHolder) holder).shop_check_num
								.setText(String.valueOf(list.get(position).getShop_check_num()));
					} else {
						ToastMessage.showToast(context, "不能再减拉...");
					}

					((MainActivity) context).getShopFragment().itemCheckBox();
				}
			});
			((ItemViewHolder) holder).shop_more.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					list.get(position).setShop_check_num(list.get(position).getShop_check_num() + 1);
					list.get(position).setShop_num(String.valueOf((list.get(position).getShop_check_num())));
					((ItemViewHolder) holder).shop_check_num
							.setText(String.valueOf(list.get(position).getShop_check_num()));

					((MainActivity) context).getShopFragment().itemCheckBox();
				}
			});
			((ItemViewHolder) holder).shop_check_num.setText(String.valueOf(list.get(position).getShop_check_num()));
			((ItemViewHolder) holder).shop_check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					list.get(position).setShop_check(isChecked);
					((ItemViewHolder) holder).shop_check.setChecked(list.get(position).getShop_check());

					((MainActivity) context).getShopFragment().itemCheckBox();
				}
			});
			((ItemViewHolder) holder).shop_check.setChecked(list.get(position).getShop_check());

			((ItemViewHolder) holder).shop_cover.setImageResource(list.get(position).getShop_cover());
			((ItemViewHolder) holder).shop_name.setText(list.get(position).getShop_name());
			((ItemViewHolder) holder).shop_size.setText(list.get(position).getShop_size());
			if (!TextUtils.isEmpty(list.get(position).getShop_price_normal())
					&& !TextUtils.equals(list.get(position).getShop_price_normal(), "null")
					&& !TextUtils.equals(list.get(position).getShop_price_normal(), "0")) {
				// ((ItemViewHolder) holder).shop_price_xian.setText("¥" + list.get(position).getShop_price_normal());

				// ((ItemViewHolder) holder).shop_price_xian.setText("¥" + list.get(position).getShop_price_xian());
				((ItemViewHolder) holder).shop_price_normal.setText("¥" + list.get(position).getShop_price_normal());
				// 中间加横线
				// ((ItemViewHolder) holder).shop_price_normal.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
				// 底部加横线：
				// ((ItemViewHolder)
				// holder).shop_price_normal.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG
				// );
			}

		}
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

		if (mHeaderView != null && viewType == TYPE_HEADER) {
			return new HeadViewHolder(mHeaderView);
		} else if (viewType == TYPE_ITEM) {
			// View view =
			// LayoutInflater.from(parent.getContext()).inflate(R.layout.shopping_list_item_text,
			// null);
			View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_shop_item, null);
			view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			return new ItemViewHolder(view);
		} else if (viewType == TYPE_FOOTER) {
			View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.shopping_footerview, null);
			view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			return new FooterViewHolder(view);
		}
		return null;
	}

	class HeadViewHolder extends RecyclerView.ViewHolder {
		TextView text;

		public HeadViewHolder(View itemView) {
			super(itemView);
			if (itemView == mHeaderView)
				return;
			text = (TextView) itemView.findViewById(R.id.text);
		}
	}

	class FooterViewHolder extends ViewHolder {

		public FooterViewHolder(View view) {
			super(view);
		}

	}

	class ItemViewHolder extends ViewHolder {
		ImageView shop_delete;
		FrameLayout shop_less;
		FrameLayout shop_more;
		TextView shop_check_num;
		CheckBox shop_check;
		ImageView shop_cover;
		TextView shop_name;
		TextView shop_size;
		TextView shop_price_xian;
		TextView shop_price_normal;

		public ItemViewHolder(View view) {
			super(view);
			shop_delete = (ImageView) view.findViewById(R.id.shop_delete);
			shop_less = (FrameLayout) view.findViewById(R.id.shop_less);
			shop_more = (FrameLayout) view.findViewById(R.id.shop_more);
			shop_check_num = (TextView) view.findViewById(R.id.shop_check_num);
			shop_check = (CheckBox) view.findViewById(R.id.shop_check);
			shop_cover = (ImageView) view.findViewById(R.id.shop_cover);
			shop_name = (TextView) view.findViewById(R.id.shop_name);
			shop_size = (TextView) view.findViewById(R.id.shop_size);
			shop_price_xian = (TextView) view.findViewById(R.id.shop_price_xian);
			shop_price_normal = (TextView) view.findViewById(R.id.shop_price_normal);
		}
	}

}
